package main;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ConnectionListener implements Runnable {
	private static final int LISTENER_PORT = 43211;
	ServerSocket serverSocket;
	boolean listen;
	Controller controller;
	
	public ConnectionListener(Controller controller) throws IOException{
		serverSocket = new ServerSocket(LISTENER_PORT);
		listen = true;
		this.controller = controller;
	}

	/**
	 * Listen for incoming game requests on serverSocket
	 * Send sockets to controller
	 */
	@Override
	public void run() {
		while(listen){
			try {
				Socket socket = serverSocket.accept();
				controller.gameRequestFromOpponent(socket);
			} catch (IOException e) {}
		}
	}
	public void stopListening(){
		listen = false;
	}
	
}